package com.woniuxy.com.demo.Util;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * description: JedisUtil <br>
 * date: 2022/1/4 5:47 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */
public class JedisUtil {
    private static JedisPool jedisPool;

//    static {
//        //版本1
//        GenericObjectPoolConfig poolConfig = new JedisPoolConfig();
//        Properties properties = new Properties();
//        //类路径
//        //InputStream inputStream = JedisUtil.class.getClassLoader().getResourceAsStream("jedis.properties");
//        //类路径
////        InputStream inputStream = JedisUtil.class.getResourceAsStream("/jedis.properties");
////        try {
////            properties.load(inputStream);
////            System.out.println(properties.getProperty("jedis.host"));
////        } catch (IOException e) {
////            e.printStackTrace();
////        }
////        poolConfig.setMaxTotal(50);
////        poolConfig.setMaxIdle(10);
////        jedisPool = new JedisPool(poolConfig, "192.168.13.19", 6379, 2000, "123");
//
//        //版本2
//        ResourceBundle resourceBundle = ResourceBundle.getBundle("jedis");
//        System.out.println(resourceBundle.getString("jedis.port"));
//        poolConfig.setMaxTotal(50);
//        poolConfig.setMaxIdle(10);
//        jedisPool = new JedisPool(poolConfig, "192.168.13.19", 6379, 2000, "123");
//    }
//解决：SpringBoot中将配置文件中属性值，注入成员变量：在静态代码块中使用
static {
    GenericObjectPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(50);
    poolConfig.setMaxIdle(10);
    jedisPool = new JedisPool(poolConfig, JedisConstant.HOST, 6379, 2000, "123");
}


    public static Jedis getJedis() {
        return jedisPool.getResource();
    }
}
